# -*- encoding: utf-8 -*-

from odoo import models, fields, api
from odoo.addons import base_cw

# 常用字段
ACCOUNT_FIELD = [
    ('local_amount_untaxed', u'未税金额'),
    ('local_amount', u'含税金额'),
    ('local_amount_tax', u'税额'),
    ('materials', u'材料费'),
    ('labor', u'人工费'),
    ('overhead', u'制造费'),
    ('subcontract', u'外协费'),
    ('total_cost', u'成本金额'),
]
LOCAL_FIELD = [
    ('local_amount_untaxed', u'本币未税金额'),
    ('local_amount_total', u'本币含税金额'),
    ('local_amount_tax', u'本币税额'),
    ('price_subtotal_signed', u'发票本币未税金额'),
    ('total_amount_signed', u'发票本币含税金额'),
    ('tax_amount_signed', u'发票本币税额'),
    ('materials', u'本币材料费'),
    ('labor', u'本币人工费'),
    ('overhead', u'本币制造费'),
    ('subcontract', u'本币外协费'),
    ('total_cost', u'本币成本金额'),
]
class AccountVoucherTemplate(models.Model):
    _inherit = 'account.voucher.template'
    _description = u'凭证模版'


    @api.model
    def _get_default_model(self):
        model_id = self.env['ir.model'].search([('model', '=', 'account.voucher')], limit=1)
        if model_id:
            return model_id.id
        return False

    @api.model
    def _get_default_voucher_category(self):
        category_id = self.env['account.voucher.category'].search([('code', '=', '09')], limit=1)
        if category_id:
            return category_id.id
        return False

    voucher_category_id = fields.Many2one('account.voucher.category', u'凭证类别')  # ,default=_get_default_voucher_category
    model_id = fields.Many2one('ir.model', u'目标类型')  # ,default=_get_default_model
    product_type_ids = fields.One2many('account.voucher.template.product.type', 'template_id')
    model_model = fields.Char('模型', related='model_id.model', store=True)
    invoice_type = fields.Selection([('sale', u'销售'),('purchase', u'采购')], u'发票类型')
    picking_type_id = fields.Many2one('stock.picking.type', '作业类型')
    model_domain = fields.Char('过滤条件')
    merge_type = fields.Selection([('month', u'按月'),('doc', u'按单据'),('detail', u'不合并')], u'凭证合并方式',default='doc')

    def get_line_ids(self, dc_type, product_type):
        res = self.line_ids.filtered(lambda r: r.dc_type == dc_type and r.product_type == product_type) \
              or self.line_ids.filtered(lambda r: r.dc_type == dc_type and not r.product_type)
        return res

    def get_d_type_line_ids(self, product_type):
        return self.get_line_ids(dc_type='D', product_type=product_type)

    def get_c_type_line_ids(self, product_type):
        return self.get_line_ids(dc_type='C', product_type=product_type)

    def get_dc_type_line_ids(self, product_type):
        res = self.get_c_type_line_ids(product_type=product_type)
        res |= self.get_d_type_line_ids(product_type=product_type)
        return res


class AccountVoucherTemplateLine(models.Model):
    _inherit = 'account.voucher.template.line'

    # categ_id = fields.Many2one('product.category', string=u'分类')
    product_type = fields.Selection(base_cw.public.PRODUCT_TYPE, u'成本类型', index=True)
    subaccount_category_ids = fields.Many2many('subaccount.category', u'辅助核算类别',related='account_id.subaccount_category_ids')
    amount_field = fields.Selection(ACCOUNT_FIELD, u'金额字段')
    local_amount_field = fields.Selection(LOCAL_FIELD, u'本币金额字段')

    def get_account_id(self):
        self.ensure_one()
        if self.account_id:
            res = self.account_id
        else:
            if self.dc_type == 'D':
                res = self.env['cncw.account'].search([
                    ('code', '=', '1405')
                ])
            else:
                res = self.env.ref('cncw_ledger.chart220202')
        return res


class AccountVoucherTemplateProductType(models.Model):
    _name = 'account.voucher.template.product.type'

    product_type = fields.Selection(base_cw.public.PRODUCT_TYPE, string=u'成本类型')
    template_id = fields.Many2one('account.voucher.template', string=u'凭证模板')

